package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.errorLog.ErrorLog;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayCmnGlassV;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayParamV;
import com.navi.loaddata.core.oracleEntity.base.EtlParamV;
import com.navi.loaddata.core.oracleEntity.base.EtlParamVMapper;
import com.navi.loaddata.core.oracleEntity.cell1.EtlCellCmnComponentV;
import com.navi.loaddata.core.oracleEntity.cell1.EtlCellParamV;
import com.navi.loaddata.core.oracleEntity.cell2.EtlCell2CmnChipV;
import com.navi.loaddata.core.oracleEntity.cell2.EtlCell2ParamV;
import com.navi.loaddata.core.oracleEntity.cf.EtlCfCmnGlassV;
import com.navi.loaddata.core.oracleEntity.cf.EtlCfParamV;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static java.util.stream.Collectors.groupingBy;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveBaseDataDao {
    private static final Logger logger = LoggerFactory.getLogger(SaveBaseDataDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;


    public void saveBaseData(EtlConfDModel etlConfD, List<EtlStepV> etlStepVList, List<EtlProductV> etlProductVList) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLStepProcedure = "";
        String SQLProductProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLStepProcedure = "{call ARRAY_BASE_LOADER.LOAD_STEP()}";
                    SQLProductProcedure = "{call ARRAY_BASE_LOADER.LOAD_PRODUCT()}";
                    dao3.saveAll(EtlStepVMapper.INSTANCE.etlStepToArray(etlStepVList));
                    dao3.saveAll(EtlProductVMapper.INSTANCE.etlProductToArray(etlProductVList));
                    break;
                case "CF":
                    SQLStepProcedure = "{call CF_BASE_LOADER.LOAD_STEP()}";
                    SQLProductProcedure = "{call CF_BASE_LOADER.LOAD_PRODUCT()}";
                    dao3.saveAll(EtlStepVMapper.INSTANCE.etlStepToCf(etlStepVList));
                    dao3.saveAll(EtlProductVMapper.INSTANCE.etlProductToCf(etlProductVList));
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLStepProcedure = "{call CELL1_BASE_LOADER.LOAD_STEP()}";
                        SQLProductProcedure = "{call CELL1_BASE_LOADER.LOAD_PRODUCT()}";
                        dao3.saveAll(EtlStepVMapper.INSTANCE.etlStepToCell1(etlStepVList));
                        dao3.saveAll(EtlProductVMapper.INSTANCE.etlProductToCell1(etlProductVList));
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLStepProcedure = "{call CELL2_BASE_LOADER.LOAD_STEP()}";
                        SQLProductProcedure = "{call CELL2_BASE_LOADER.LOAD_PRODUCT()}";
                        dao3.saveAll(EtlStepVMapper.INSTANCE.etlStepToCell2(etlStepVList));
                        dao3.saveAll(EtlProductVMapper.INSTANCE.etlProductToCell2(etlProductVList));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_STEPANDPRODUCT,e);
        }

        // call base proc
        try {
            SQLQuery queryLoadStep = session.createSQLQuery(SQLStepProcedure);
            queryLoadStep.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_STEP,e);
        }
        try {
            SQLQuery queryLoadProduct = session.createSQLQuery(SQLProductProcedure);
            queryLoadProduct.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_PRODUCT,e);
        }
    }


    public void saveCmnGlass(EtlConfDModel etlConfD, List<EtlCmnGlassV> etlCmnGlassVList) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLCmnGlassProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLCmnGlassProcedure = "{call ARRAY_MES_LOADER.LOAD_CMN_GLASS()}";
                    dao3.saveAll(EtlCmnGlassVMapper.INSTANCE.etlCmnGlassToArray(etlCmnGlassVList));
                    break;
                case "CF":
                    SQLCmnGlassProcedure = "{call CF_MES_LOADER.LOAD_CMN_GLASS()}";
                    dao3.saveAll(EtlCmnGlassVMapper.INSTANCE.etlCmnGlassToCf(etlCmnGlassVList));

                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLCmnGlassProcedure = "{call CELL1_MES_LOADER.LOAD_CMN_COMPONENT()}";
                        dao3.saveAll(EtlCmnGlassVMapper.INSTANCE.etlCmnGlassToCell1(etlCmnGlassVList));
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLCmnGlassProcedure = "{call CELL2_MES_LOADER.LOAD_CMN_CHIP()}";
                        dao3.saveAll(EtlCmnGlassVMapper.INSTANCE.etlCmnGlassToCell2(etlCmnGlassVList));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_HIS_CMN_GLASS,e);
        }

        // call cmn proc
        try {
            SQLQuery queryLoadCmnGlass = session.createSQLQuery(SQLCmnGlassProcedure);
            queryLoadCmnGlass.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_CMN_GLASS,e);
        }
    }

    public void deleteCmnGlass(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String hql = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hql = "Delete FROM EtlArrayCmnGlassV";
                    break;
                case "CF":
                    hql = "Delete FROM EtlCfCmnGlassV";
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hql = "Delete FROM EtlCellCmnComponentV";
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        hql = "Delete FROM EtlCell2CmnChipV";
                    }
                    break;
            }
            Query qHql = session.createQuery(hql);
            qHql.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_HIS_CMN_GLASS,e);
        }
    }


    public void saveParamData(EtlConfDModel etlConfD, List<EtlParamV> etlParamList) throws Exception{
        Session session = dao3.getEntityManagerSession();
        String SQLParamProcedure="";
        try{
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLParamProcedure = "{Call ARRAY_BASE_LOADER.LOAD_PARAM()}";
                    dao3.saveAll(EtlParamVMapper.INSTANCE.etlParamToArray(etlParamList));
                    break;
                case "CF":
                    SQLParamProcedure = "{Call CF_BASE_LOADER.LOAD_PARAM()}";
                    dao3.saveAll(EtlParamVMapper.INSTANCE.etlParamToCf(etlParamList));
                    break;
                case "OC":
                    if("CELL1".equals(etlConfD.getCellType())){
                        SQLParamProcedure = "{Call CELL1_BASE_LOADER.LOAD_PARAM()}";
                        dao3.saveAll(EtlParamVMapper.INSTANCE.etlParamToCell(etlParamList));
                    }else if("CELL2".equals(etlConfD.getCellType())){
                        SQLParamProcedure = "{Call CELL2_BASE_LOADER.LOAD_PARAM()}";
                        dao3.saveAll(EtlParamVMapper.INSTANCE.etlParamToCell2(etlParamList));
                    }
                    break;
            }
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_PARAM,e);
        }

        try {
            SQLQuery queryParam = session.createSQLQuery(SQLParamProcedure);
            queryParam.executeUpdate();
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_PARAM,e);
        }
    }

    public void deleteTeg2CmnAndParam(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        try {
            String hqlCmnGlass = "";
            String hqlParam = "";
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlCmnGlass ="Delete FROM EtlArrayCmnGlassV";
                    hqlParam="Delete FROM EtlArrayParamV";
                    break;
                case "CF":
                    hqlCmnGlass ="Delete FROM EtlCfCmnGlassV";
                    hqlParam="Delete FROM EtlCfParamV";
                    break;
                case "OC":
                    if("CELL1".equals(etlConfD.getCellType())){
                        hqlCmnGlass ="Delete FROM EtlCellCmnComponentV";
                        hqlParam="Delete FROM EtlCellParamV";
                    }else if("CELL2".equals(etlConfD.getCellType())){
                        hqlCmnGlass ="Delete FROM EtlCell2CmnChipV";
                        hqlParam="Delete FROM EtlCell2ParamV";
                    }
                    break;
            }
            Query queryCmnGlass = session.createQuery(hqlCmnGlass);
            Query queryParam = session.createQuery(hqlParam);

            queryCmnGlass.executeUpdate();
            queryParam.executeUpdate();
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_TEG2_CMN,e);
        }
    }


    public void deleteParam(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        try {
            String hqlParam = "";
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlParam="Delete FROM EtlArrayParamV";
                    break;
                case "CF":
                    hqlParam="Delete FROM EtlCfParamV";
                    break;
                case "OC":
                    if("CELL1".equals(etlConfD.getCellType())){
                        hqlParam="Delete FROM EtlCellParamV";
                    }else if("CELL2".equals(etlConfD.getCellType())){
                        hqlParam="Delete FROM EtlCell2ParamV";
                    }
                    break;
            }
            Query queryParam = session.createQuery(hqlParam);
            queryParam.executeUpdate();
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_PARAM,e);
        }
    }
}
